from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.hashers import check_password
from django.core.paginator import Paginator
from .serializers import *
from django.db.models import Q

# 登录
class LoginView(APIView):

    def post(self,request):
        # 1
        username = request.data.get('username','')
        password = request.data.get('password','')
        # 2
        userobj = models.User.objects.filter(username=username).first()

        if not userobj:

            return Response({"code":1001,"msg":"用户名不存在"})

        pwd = userobj.password

        # 3
        if check_password(password,pwd):
            data = {
                "uid": userobj.id,
                "auth": userobj.auth
            }
            return Response({"code":1000,"msg":"登录成功","data":data})
        return Response({"code":1002,"msg":"用户名或者密码错误"})


# 员工
class UserView(APIView):
    # 查询
    def get(self,request):
        # 1
        p = request.GET.get("p",1)
        keyword = request.GET.get("keyword","").strip()
        # 2 查数据
        userqueryset = models.User.objects.filter(Q(username__contains=keyword)|Q(depart__name__contains=keyword)).all()
        # 3 实例化分页
        page_obj = Paginator(userqueryset,per_page=2)
        # 4 获取分页的范围
        page = [i for i in page_obj.page_range]
        # 5 获取当前页的数据
        page_data = page_obj.page(p)
        # 6 判断上一页和下一页
        previous = page_data.has_previous()
        next = page_data.has_next()
        # 7 序列化当前页的数据
        obj = UserSerializer(page_data,many=True)
        # 8 构造数据
        data = {
            "data":obj.data,
            "page":page,
            "previous":previous,
            "next":next
        }
        # 9  返回
        return Response({"code":1000,"msg":"","data":data})





        pass


    def post(self,request):
        # 1 接受参数
        username = request.data.get('username','').strip()
        password = request.data.get('password','').strip()
        depart = request.data.get('depart','0')

        # 2 验证数据重复性
        userobj = models.User.objects.filter(username=username).first()

        if userobj:
            return Response({"code":1001,"msg":"数据已存在"})

        # 3 构造数据
        data = {
            "username":username,
            "password":password,
            "depart":depart
        }

        # 4 反序列化
        obj = UserSerializer(data=data)

        # 5 判断
        if obj.is_valid():
            obj.save()
            return Response({"code": 1000, "msg": "添加成功"})

        # 6 打印信息
        print(obj.errors)

        # 7 打印
        return Response({"code":1002,"msg":"参数有误"})


# 部门
class DepartmentView(APIView):

    # 查询
    def get(self,request):
        dequeryset  =  models.Department.objects.all()
        obj = DepartmentSerializer(dequeryset,many=True)
        return Response({"code":1000,"msg":"","data":obj.data})


    # 添加
    def post(self,request):
        # 1
        name = request.data.get("name")

        # 2 重复性
        deobj = models.Department.objects.filter(name=name).first()
        if deobj:
            return Response({"code":1001,"msg":"数据已存在"})

        # 3 构造数据
        data = {
            "name":name
        }
        # 4 反序列化
        obj = DepartmentSerializer(data=data)

        # 5 判断
        if obj.is_valid():
            obj.save()
            return Response({"code":1000,"msg":"添加成功"})
        # 6 打印信息
        print(obj.errors)
        # 7 返回
        return Response({"code":1002,"msg":"参数有误"})